import { createApp } from 'vue'
import App from "@/App.vue";
//引入plus
import plus, {ElMessage} from 'element-plus';
import 'element-plus/dist/index.css';
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
//引入路由js
import router from './router';
//引入store
import store from "./store";
//导入axios
import axios from 'axios';
//配置后端统一地址
axios.defaults.baseURL = 'http://localhost:8081/';
//配置统一请求头
axios.interceptors.request.use(config=>{
    //获取token
    let token = sessionStorage.getItem('token');
    if (token){
        config.headers.token = token
    }
    return config;
})
//配置响应数据
axios.interceptors.response.use(resp=>{
    if (resp.data.code === 401){//不认证
        ElMessage.error(resp.data.msg)
        router.push('/login')
        return Promise.reject();
    }
    if (resp.data.code === 1002){//会话过期
        ElMessage.error(resp.data.msg)
        sessionStorage.clear()
        location.reload(); // 强制刷新整个页面
        return;
    }
    if (resp.data.code != 200){//会话过期
        ElMessage.error(resp.data.msg)
        return Promise.reject(new Error(resp.data.msg));
    }
    return resp.data
})

let app = createApp(App);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
app.use(plus,{ locale: zhCn }).use(router).use(store).mount('#app')